//
//  ImageScroll.m
//  ImagePreview
//
//  Created by wujungao on 15/6/8.
//  Copyright (c) 2015年 wjg. All rights reserved.
//

#import "ImageScroll.h"

@interface ImageScroll()

-(void)initLogic:(NSArray *)array;

@end

@implementation ImageScroll

/* 初始化 */
//多图浏览
-(UIScrollView *)initWithImagesArray:(NSArray *)imagesArray{
    self=[super initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, [UIScreen mainScreen].applicationFrame.size.height)];
    if(self){
        [self setScreenValue];
        [self initLogic:imagesArray];
        
        self.delegate=self;
        self.pagingEnabled=YES;
    }
    return self;
}

-(void)setScreenValue{
    self.screenWidth=[UIScreen mainScreen].bounds.size.width;
    self.screenAppHight=[UIScreen mainScreen].applicationFrame.size.height;
}

/* 胶合逻辑 */
-(void)initLogic:(NSArray *)array{
    self.imagesArray=array;
    
    if(self.imagesArray.count==1){//单图预览
        VIPhotoView *imagePreview=[[VIPhotoView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, [UIScreen mainScreen].applicationFrame.size.height) andImage:[self.imagesArray objectAtIndex:0]];
        
        self.contentSize=imagePreview.bounds.size;
        [self addSubview:imagePreview];
        
    }else{//多图预览
        if(self.imagesArray.count==0){//如果为空，则退出图片预览效果
            return;
        }
        
        [self multiImagesPreview];
    }
}

/*
 wjg,20150609:
    此方法是：有多少图就立马创建多少个scrollview并且同时把图加载到内存。
    未来应该优化次方法，使用lazyload以及复用object方式
 */
-(void)multiImagesPreview{
    for(NSInteger i=0;i<self.imagesArray.count;i++){
        
//        SingleImageView *objectName=[[SingleImageView alloc] initWithImage:self.imagesArray[i]];
//        objectName.tag=i;//标记不同的view
//        objectName.frame=CGRectMake(self.screenWidth * i, 0, self.screenWidth, self.screenAppHight);
//        
//        [self addSubview:objectName];
        VIPhotoView *objectName=[[VIPhotoView alloc] initWithFrame:CGRectMake(self.screenWidth * i, 0, self.screenWidth, self.screenAppHight) andImage:[self.imagesArray objectAtIndex:i]];
        objectName.tag=i;
        [self addSubview:objectName];
    }
    
    self.contentSize=CGSizeMake(self.imagesArray.count * self.screenWidth, self.screenAppHight);
}

@end